var crypto = require('../utils/crypto');
var express = require('express');

var http = require('../utils/http');



var app = express();
var config = null;

var serverIp = "";

//测试
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});



app.get('/ping', function (req, res) {
    var sign = req.query.sign;
    var md5 = crypto.md5(config.ROOM_PRI_KEY);
    if (md5 != sign) {
        return;
    }
    http.send(res, 0, "pong");
});



var gameServerInfo = null;
var lastTickTime = 0;

//向大厅服定时心跳
function update() {
    if (lastTickTime + config.HTTP_TICK_TIME < Date.now()) {
        lastTickTime = Date.now();
       gameServerInfo.load = 1;
        http.get(config.HALL_IP, config.HALL_PORT, "/register_gs", gameServerInfo, function (ret, data) {
            if (ret == true) {
                if (data.errcode != 0) {
                    console.log(data.errmsg);
                }

                if (data.ip != null) {
                    serverIp = data.ip;
                }
            }
            else {
                //
                lastTickTime = 0;
            }
        });

        var mem = process.memoryUsage();
        var format = function (bytes) {
            return (bytes / 1024 / 1024).toFixed(2) + 'MB';
        };
        //console.log('Process: heapTotal '+format(mem.heapTotal) + ' heapUsed ' + format(mem.heapUsed) + ' rss ' + format(mem.rss));
    }
}

exports.start = function ($config) {
    config = $config;

    //
    gameServerInfo = {
        id: config.SERVER_ID,
        clientip: config.CLIENT_IP,
        clientport: config.CLIENT_PORT,
        httpPort: config.HTTP_PORT,
        load:1,
    };

    setInterval(update, 1000);
    app.listen(config.HTTP_PORT, config.FOR_HALL_IP);
    console.log("http server is listening on " + config.FOR_HALL_IP + ":" + config.HTTP_PORT);
};